use crate::shared::primes::miller_rabin;

#[must_use]
pub fn min_edge_prime_num(percentage: u32) -> String {
    if percentage == 0 || percentage > 100 {
        return String::new();
    }

    let mut primes: u128 = 0;
    let mut n: u128 = 3;

    loop {
        let total = 2 * n - 1;
        let x1 = n * n - 3 * (n - 1);
        let x2 = n * n - 2 * (n - 1);
        let x3 = n * n - (n - 1);
        for x in [x1, x2, x3] {
            if miller_rabin(x) {
                primes += 1;
            }
        }
        let rate = (primes as f64) / (total as f64);
        if rate < (f64::from(percentage) / 100.0) {
            return format!("{n},{primes}");
        }
        n += 2;
    }
}

// const TABLE: &[(u32, u32, u32, u32, f64)] = &[
//     (100, 3, 3, 5, 0.6000),
//     (99, 3, 3, 5, 0.6000),
//     (98, 3, 3, 5, 0.6000),
//     (97, 3, 3, 5, 0.6000),
//     (96, 3, 3, 5, 0.6000),
//     (95, 3, 3, 5, 0.6000),
//     (94, 3, 3, 5, 0.6000),
//     (93, 3, 3, 5, 0.6000),
//     (92, 3, 3, 5, 0.6000),
//     (91, 3, 3, 5, 0.6000),
//     (90, 3, 3, 5, 0.6000),
//     (89, 3, 3, 5, 0.6000),
//     (88, 3, 3, 5, 0.6000),
//     (87, 3, 3, 5, 0.6000),
//     (86, 3, 3, 5, 0.6000),
//     (85, 3, 3, 5, 0.6000),
//     (84, 3, 3, 5, 0.6000),
//     (83, 3, 3, 5, 0.6000),
//     (82, 3, 3, 5, 0.6000),
//     (81, 3, 3, 5, 0.6000),
//     (80, 3, 3, 5, 0.6000),
//     (79, 3, 3, 5, 0.6000),
//     (78, 3, 3, 5, 0.6000),
//     (77, 3, 3, 5, 0.6000),
//     (76, 3, 3, 5, 0.6000),
//     (75, 3, 3, 5, 0.6000),
//     (74, 3, 3, 5, 0.6000),
//     (73, 3, 3, 5, 0.6000),
//     (72, 3, 3, 5, 0.6000),
//     (71, 3, 3, 5, 0.6000),
//     (70, 3, 3, 5, 0.6000),
//     (69, 3, 3, 5, 0.6000),
//     (68, 3, 3, 5, 0.6000),
//     (67, 3, 3, 5, 0.6000),
//     (66, 3, 3, 5, 0.6000),
//     (65, 3, 3, 5, 0.6000),
//     (64, 3, 3, 5, 0.6000),
//     (63, 3, 3, 5, 0.6000),
//     (62, 3, 3, 5, 0.6000),
//     (61, 3, 3, 5, 0.6000),
//     (60, 5, 5, 9, 0.5556),
//     (59, 5, 5, 9, 0.5556),
//     (58, 5, 5, 9, 0.5556),
//     (57, 5, 5, 9, 0.5556),
//     (56, 5, 5, 9, 0.5556),
//     (55, 9, 9, 17, 0.5294),
//     (54, 9, 9, 17, 0.5294),
//     (53, 9, 9, 17, 0.5294),
//     (52, 11, 10, 21, 0.4762),
//     (51, 11, 10, 21, 0.4762),
//     (50, 11, 10, 21, 0.4762),
//     (49, 11, 10, 21, 0.4762),
//     (48, 11, 10, 21, 0.4762),
//     (47, 13, 11, 25, 0.4400),
//     (46, 13, 11, 25, 0.4400),
//     (45, 13, 11, 25, 0.4400),
//     (44, 19, 16, 37, 0.4324),
//     (43, 23, 19, 45, 0.4222),
//     (42, 27, 22, 53, 0.4151),
//     (41, 29, 23, 57, 0.4035),
//     (40, 31, 23, 61, 0.3770),
//     (39, 31, 23, 61, 0.3770),
//     (38, 31, 23, 61, 0.3770),
//     (37, 33, 23, 65, 0.3538),
//     (36, 33, 23, 65, 0.3538),
//     (35, 35, 24, 69, 0.3478),
//     (34, 39, 26, 77, 0.3377),
//     (33, 43, 28, 85, 0.3294),
//     (32, 45, 28, 89, 0.3146),
//     (31, 47, 28, 93, 0.3011),
//     (30, 49, 28, 97, 0.2887),
//     (29, 49, 28, 97, 0.2887),
//     (28, 53, 29, 105, 0.2762),
//     (27, 71, 38, 141, 0.2695),
//     (26, 89, 45, 177, 0.2542),
//     (25, 99, 49, 197, 0.2487),
//     (24, 109, 52, 217, 0.2396),
//     (23, 197, 90, 393, 0.2290),
//     (22, 227, 99, 453, 0.2185),
//     (21, 259, 108, 517, 0.2089),
//     (20, 309, 123, 617, 0.1994),
//     (19, 363, 137, 725, 0.1890),
//     (18, 463, 166, 925, 0.1795),
//     (17, 525, 178, 1049, 0.1697),
//     (16, 723, 231, 1445, 0.1599),
//     (15, 981, 294, 1961, 0.1499),
//     (14, 1883, 527, 3765, 0.1400),
//     (13, 3593, 934, 7185, 0.1300),
//     (12, 6523, 1565, 13045, 0.1200),
//     (11, 12201, 2684, 24401, 0.1100),
//     (10, 26241, 5248, 52481, 0.1000),
//     (9, 74373, 13387, 148745, 0.0900),
//     (8, 238733, 38197, 477465, 0.0800),
//     (7, 1213001, 169820, 2426001, 0.0700),
//     (6, 10273343, 1232801, 20546685, 0.0600),
// ];
